#!/usr/bin/env zsh
# Description:
#   Specify whether to register the package as plugins or commands

local    arg="$1" package
local -a parsed_zplugs
local    as
local    default="plugin"
local -a candidates

candidates=(
"$default"
"command"
"theme"
)

package="${arg}, ${zplugs[$arg]%, }"
parsed_zplugs=(${(s/, /)package/,  */, })

as="${parsed_zplugs[(k)as:*]#as:*}"
if [[ -z $as ]]; then
    zstyle -t ":zplug:tag" as "${candidates[@]}"
    case $status in
        0)
            # ok
            zstyle -s ":zplug:tag" as as
            ;;
        1)
            __zplug::io::print::f \
                --die \
                --zplug \
                --error \
                --func \
                "as tag must be [%s] ($fg[green]%s$reset_color)\n" \
                "${(j:, :)candidates[*]}" \
                "$arg"
            return 1
            ;;
        2)
            # undefined
            ;;
    esac
fi

: ${as:=$default}
if [[ ! $as =~ ^(${(j:|:)candidates[@]})$ ]]; then
    __zplug::io::print::f \
        --die \
        --zplug \
        --error \
        --func \
        "as tag must be [%s] ($fg[green]%s$reset_color)\n" \
        "${(j:, :)candidates[*]}" \
        "$arg"
    return 1
fi

echo "$as"
